﻿using System;
using System.Collections.Generic;
using System.Text;
using CadStrategy2013.DxfData;
using MyIni;

namespace CadStrategy2013.CgqStyle
{
    public class ZLClass:Cgq2013
    {
        public ZLClass(string secname)
            : base(secname)
        {
        }
        public ZLClass(string secname, Dictionary<string, string> dic)
            :base(secname,dic)
        {
        }
        //public override void GetCfg()
        //{
        //    base.GetCfg();
        //    GetCfg("站联");
        //}
        public override DxfDataClass GetDxfData()
        {
            DxfDataClass dxfDataClass = new DxfDataClass();
            IniFile inifile = new IniFile("./\\" + StationName + "\\bzdzl.txt");
            int sum0 = inifile.GetInt16("站联", "sum", 0);
            for (int i = 0; i < sum0; i++)
            {
                DevClass devClass = new DevClass();
                string str = inifile.GetString("站联", (i + 1).ToString(), "");
                string[] strs = str.Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
                if (strs.Length > 2)
                {
                    devClass.DevName = strs[0];
                    devClass.peiList.Add(strs[1]);
                    devClass.peiList.Add(strs[2]);
                }
                else
                {
                    devClass.DevName = strs[0];
                    devClass.peiList.Add("");
                    devClass.peiList.Add("");
                }
                dxfDataClass.devList.Add(devClass);
            }
            if (sum0 != 0)
            {
                ShiJiBanCount = (dxfDataClass.devList.Count-1) / BanDevCount + 1;
                ShiJiTuZhiCount = (ShiJiBanCount-1) / BanCount + 1;
            }
            else
            {
                ShiJiBanCount = 0;
                ShiJiTuZhiCount = 0;
            }
            m_dxfDataClass = dxfDataClass;
            return dxfDataClass;
        }
        public override List<string> DealData(List<string> strList,int fi)
        {
            List<string> list = new List<string>();
            int n = 0;
            foreach (string str in strList)
            {
                string str1 = str;
                str1 = ReplacebNo(str1,fi);
                str1 = ReplaceDev(str1,fi);
                str1 = ReplacePei(str1,fi);
                list.Add(str1);
                OnDealData(this, new WriteDxfArgs(SecName + fi.ToString(), str, strList.Count, ++n));
            }
            return list;
        }
        //private string[] GetBianHao()
        //{
        //    if (ShiJiBanHao == null)
        //    {
        //        string[] str = new string[ShiJiBanCount];
        //        for (int i = 0; i < ShiJiBanCount; i++)
        //        {
        //            Strategy2013.SetBianHao();
        //            str[i] = Strategy2013.bNo;
        //        }
        //        ShiJiBanHao = str;
        //    }
        //    return ShiJiBanHao;
        //}
        /// <summary>
        /// 因为站联电压起始编号不是从1开始，所以要重写
        /// </summary>
        /// <param name="str"></param>
        /// <param name="fi"></param>
        /// <returns></returns>
        public override string ReplacebNo(string str,int fi)
        {
            string[] strs = GetBianHao();
            for (int i = 3; i <= BanCount+2; i++)
            {
                int nj = i;// -fi * BanCount;
                string str1 = "bNo" + nj.ToString() + ">";
                if (str.Contains(str1))
                {
                    if ((i + fi * BanCount) <= ShiJiBanCount+2)
                    {
                        str = str.Replace(str1, strs[(fi*BanCount)+i - 3]);
                    }
                    else
                    {
                        str = "";
                    }
                }
            }
            return str;
        }
        public override  string ReplaceDev(string str2,int fi)
        {
            string str = str2;
            for (int j = 3; j <= BanCount+2; j++)
            {
                int nj = j;// -fi * BanCount + 2;
                //int n = j + 2;
                for (int i = 1; i <= BanDevCount; i++)
                {
                    string str1 = "dev" + nj.ToString() + "-" + i.ToString()+">";
                    if (str.Contains(str1))
                    {
                        //int index = (j - 3) * BanDevCount + i - 1;
                        int index = (fi * BanCount + j - 3) * BanDevCount + i - 1;
                        if (index >= m_dxfDataClass.devList.Count)
                            return "";
                        try
                        {
                            str = str.Replace(str1, m_dxfDataClass.devList[index].DevName);
                            return str;
                        }
                        catch 
                        {
                            return "";
                        }
                    }
                }
            }
            return str;
        }
        public override  string ReplacePei(string str,int fi)
        {
            for (int j = 3; j <= BanCount+2; j++)
            {
                int nj = j;// -fi * BanCount + 2;
                //int n = j + 2;
                for (int i = 1; i <= 2 * BanDevCount; i++)
                {
                    string str1 = "pei" + nj.ToString() + "-" + i.ToString()+">";
                    if (str.Contains(str1))
                    {
                        //int index = (j - 3) * BanDevCount + (i - 1) / 2;
                        int index = (fi * BanCount + j - 3) * BanDevCount + (i - 1) / BanDevPeiCount;
                        if (index >= m_dxfDataClass.devList.Count)
                            return "";
                        int index1 = (i - 1) % 2;
                        try
                        {
                            str = str.Replace(str1, m_dxfDataClass.devList[index].peiList[index1]);
                        }
                        catch 
                        {
                            return "";
                        }
                    }
                }
            }
            return str;
        }
    }
}
